from dataclasses import dataclass
from typing import Generic, TypeVar, Optional

from common.exception.biz_exception import BizException
from common.response.biz_response_code import SUCCESS

T = TypeVar('T')


@dataclass
class R(Generic[T]):
    data: Optional[T] = None
    code: int = 0
    message: str = ""

    @staticmethod
    def success(data: Optional[T] = None, message: Optional[str] = None) -> 'R[T]':
        return R(data=data, code=SUCCESS.code, message=message if message is not None else SUCCESS.msg)

    @staticmethod
    def failed(exception: BizException) -> 'R[T]':
        return R(code=exception.code.code, message=str(exception))
